package com.zhuwhao.sample.utils;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.zhuwhao.sample.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by zhuwh on 2016/12/10 0010.
 * 日期时间选择器工具类
 */
public class DateTimePickerUtils implements OnDateSetListener {

    private Context context;

    private String oldDateTime;

    private Type type;

    private OnDateTimeSelectListener listener;

    public DateTimePickerUtils(Context context, String oldDateTime, Type type, OnDateTimeSelectListener listener) {
        this.context = context;
        this.oldDateTime = oldDateTime;
        this.type = type;
        this.listener = listener;
    }

    public void show(FragmentManager manager) {
        long currentTime;
        try {
            currentTime = getFormat().parse(oldDateTime).getTime();
        } catch (Exception e) {
            currentTime = System.currentTimeMillis();
        }

        TimePickerDialog dialog = new TimePickerDialog.Builder()
                .setCancelStringId("取消")
                .setTitleStringId("选择时间")
                .setSureStringId("确定")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCurrentMillseconds(currentTime)
                .setType(type)
                .setCyclic(false)
                .setThemeColor(ContextCompat.getColor(context, R.color.colorAccent))
                .setWheelItemTextSize(12)
                .setCallBack(this)
                .build();
        dialog.show(manager, "TimePickerDialog");
    }

    private SimpleDateFormat getFormat() {
        SimpleDateFormat format;
        switch (type) {
            case ALL:
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                break;
            case YEAR_MONTH_DAY:
                format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
                break;
            case HOURS_MINS:
                format = new SimpleDateFormat("HH:mm", Locale.CHINA);
                break;
            case MONTH_DAY_HOUR_MIN:
                format = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA);
                break;
            case YEAR_MONTH:
                format = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
                break;
            case YEAR:
                format = new SimpleDateFormat("yyyy", Locale.CHINA);
                break;
            default:
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                break;
        }
        return format;
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millSeconds) {
        listener.onSelect(getFormat().format(new Date(millSeconds)));
    }

    /**
     * 回调接口
     */
    public interface OnDateTimeSelectListener {
        void onSelect(String dateTime);
    }
}